package per.yhh.xxxtsj.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import per.yhh.xxxtsj.entity.Good;
import per.yhh.xxxtsj.entity.Myorder;
import per.yhh.xxxtsj.entity.Orderitem;
import per.yhh.xxxtsj.entity.User;
import per.yhh.xxxtsj.mapper.OrderitemMapper;
import per.yhh.xxxtsj.service.OrderitemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yhh
 * @since 2022-04-10
 */
@Service
public class OrderitemServiceImpl extends ServiceImpl<OrderitemMapper, Orderitem> implements OrderitemService {

    @Autowired
    OrderitemMapper orderitemMapper;

    @Override
    public Boolean buy(User user, Good good, Myorder myorder) {
        Orderitem orderitem = new Orderitem();
        orderitem.setGid(Math.toIntExact(good.getGid()));
        orderitem.setOid(Math.toIntExact(myorder.getOid()));
        orderitem.setOid(Math.toIntExact(1));
        orderitem.setNums(1);
        orderitem.setPrice(good.getPrice());
        orderitem.setTotal(good.getPrice().multiply(BigDecimal.valueOf(orderitem.getNums())));
        orderitemMapper.insert(orderitem);
        return true;
    }
}
